package com.nl.controller.web.service;

import com.nl.controller.common.annotation.SystemServiceLog;
import com.nl.controller.common.exception.SmsException;
import com.nl.controller.common.service.CommonService;

import com.nl.controller.web.bean.SmsTemplateCheck;

import com.nl.controller.web.mapper.SmsTemplateCheckMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class SmsTemplateCheckService {
    @Autowired
    private SmsTemplateCheckMapper smsTemplateCheckMapper;

    @Autowired
    private CommonService commonService;


    @SystemServiceLog(description = "短信项目模板审核", value = false)
    public int templateCheck(String mgrId, String state, String remark, String userId) {
        int result = 0;
        SmsTemplateCheck smsTemplateCheck = new SmsTemplateCheck();
        smsTemplateCheck.setRemark(remark);
        smsTemplateCheck.setCheckUser(userId);
        smsTemplateCheck.setId(mgrId);
        Map<String, Object> map = new HashMap();
        map.put("state", state);
        map.put("mgrId", mgrId);

        try {
            smsTemplateCheckMapper.modifyTemplateStatus(map);
            commonService.createSmsOperLog("3", "修改模板属性"+mgrId, "0", "短信模板审核");
            result = smsTemplateCheckMapper.insertTemplateCheck(smsTemplateCheck);
            commonService.createSmsOperLog("2", "添加审核信息"+mgrId, "0", "短信模板审核");
        } catch (Exception e) {
            e.printStackTrace();
            commonService.createSmsOperLog("3", "修改模板属性"+mgrId, "1", "短信模板审核");
            commonService.createSmsOperLog("2", "添加审核信息"+mgrId, "1", "短信模板审核");
            log.error("模板审核出现异常" + mgrId);
            throw new SmsException("模板审核出现异常" + mgrId);
        }
        return result;
    }
    
  //不入日志
    public int templateCheckJK(String mgrId, String state, String remark, String userId) {
        int result = 0;
        SmsTemplateCheck smsTemplateCheck = new SmsTemplateCheck();
        smsTemplateCheck.setRemark(remark);
        smsTemplateCheck.setCheckUser(userId);
        smsTemplateCheck.setId(mgrId);
        Map<String, Object> map = new HashMap();
        map.put("state", state);
        map.put("mgrId", mgrId);

        try {
        	//修改审核状态
            smsTemplateCheckMapper.modifyTemplateStatus(map);
            //入项目审核记录表
            result = smsTemplateCheckMapper.insertTemplateCheck(smsTemplateCheck);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("模板审核出现异常" + mgrId);
            throw new SmsException("模板审核出现异常" + mgrId);
        }
        return result;
    }
}
